package com.chenjian.controller;

import com.chenjian.entity.ArticleEntity;
import com.chenjian.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * @author TC2952
 */
@Controller
public class PageController {
    @Autowired
    private ArticleService articleServiceImpl;

    /**
     * 页面跳转
     *
     * @return
     */
    @GetMapping(value = "/")
    public String goToPage(Model model, Integer page, Integer size) {
        if (null != page) {
            if (page < 0) {
                page = 0;
            }
        } else {
            page = 0;
        }
        if (null != size) {
            if (size < 0) {
                size = 2;
            }
        } else {
            size = 2;
        }

        Page<ArticleEntity> pages = articleServiceImpl.findByPage(page, size);
        List<ArticleEntity> articleEntityList = pages.getContent();
        model.addAttribute("articleEntityList", articleEntityList);
        model.addAttribute("first", pages.isFirst());
        model.addAttribute("last", pages.isLast());
        //当前页
        model.addAttribute("page", pages.getNumber());

        return "client/index";
    }
}
